cmake_minimum_required(VERSION 3.14)

project(judger VERSION 1.0.4 LANGUAGES C CXX)

set(TARGET judger)

message("MINGW=${MINGW}")
message("CYGWIN=${CYGWIN}")
message("WIN32=${WIN32}")
message("MSVC=${MSVC}")
message("CMAKE_CXX_COMPILER_ID=${CMAKE_CXX_COMPILER_ID}")
message("CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}")
message("CMAKE_SYSTEM=${CMAKE_SYSTEM}")
message("CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}")

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/release)
set(CMAKE_INSTALL_LIBDIR ${CMAKE_BINARY_DIR}/lib)

include(FetchContent)

set(LIB_CLI_NAME  libcli)
set(LIB_CLI_GIT_TAG  master)
set(LIB_CLI_GIT_URL  https://gitee.com/jungle/${LIB_CLI_NAME}.git)
FetchContent_Declare(
	${LIB_CLI_NAME}
	GIT_REPOSITORY    ${LIB_CLI_GIT_URL}
	GIT_TAG           ${LIB_CLI_GIT_TAG}
)
FetchContent_MakeAvailable(${LIB_CLI_NAME})

set(LIB_NAME  online-judge-libs)
set(LIB_GIT_TAG  v0.0.2)
set(LIB_GIT_URL  https://gitee.com/jungle/${LIB_NAME}.git)
set(LIB_PATH_NAME  ${CMAKE_CURRENT_BINARY_DIR}/_deps/${LIB_NAME}-src)
FetchContent_Declare(
  ${LIB_NAME}
  GIT_REPOSITORY    ${LIB_GIT_URL}
  GIT_TAG           ${LIB_GIT_TAG}
)
FetchContent_MakeAvailable(${LIB_NAME})

if (GTEST_ENABLE)
	set(LIB_GTEST_NAME  googletest)
	set(LIB_GTEST_GIT_TAG  v1.14.0)
	set(LIB_GTEST_GIT_URL  https://gitee.com/jungle/${LIB_GTEST_NAME}.git)
	FetchContent_Declare(
		${LIB_GTEST_NAME}
		GIT_REPOSITORY    ${LIB_GTEST_GIT_URL}
		GIT_TAG           ${LIB_GTEST_GIT_TAG}
	)
	# For Windows: Prevent overriding the parent project's compiler/linker settings
	set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
	FetchContent_MakeAvailable(${LIB_GTEST_NAME})

if (LIBYANG_ENABLE)
	set(LIBYANG_NAME  yang)
	set(LIBYANG_TAG  v3.1.0)
	set(LIBYANG_GIT_URL  https://gitee.com/jungle/libyang.git)
	FetchContent_Declare(
	${LIBYANG_NAME}
	GIT_REPOSITORY    ${LIBYANG_GIT_URL}
	GIT_TAG           ${LIBYANG_TAG}
	)
	FetchContent_MakeAvailable(${LIBYANG_NAME})
	if(${LIBYANG_NAME}_BUILD_SUCCESS)
		# 构建成功后的自定义命令
		set_target_properties(${LIBYANG_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
	endif()
	endif()
endif()

if (MINGW)
	add_definitions("-DMINGW")	
	set(EXTRA_LIBS_DIR "${LIB_PATH_NAME}/MinGW/lib")
	set(COMPILE_DEFAULT_OPTION "-w -g -c -m32 -DWIN32 -std=gnu++0x")
	set(EXTRA_DEFAULT_LIBS -lwsock32 -lcurl -lwldap32 -lssl -lcrypto -lws2_32 mysql dbghelp psapi)
	set(LINK_STATIC_OPT -static)
	set(MODULE_LIB_TYPE )
elseif (CYGWIN)
	add_definitions("-DCYGWIN")
elseif (MSVC)
	add_definitions("-DMSVC")
	set(EXTRA_LIBS_DIR "${LIB_PATH_NAME}/VSBuild/lib")
	set(COMPILE_DEFAULT_OPTION "/nologo /W0 /wd4127 /FD /c /EHsc /MD /Zi /DWIN32 /DHAVE_CONFIG_H /DPCRE_STATIC")
	set(EXTRA_DEFAULT_LIBS 
		User32.lib ws2_32.lib winmm.lib wldap32.lib psapi.lib crypt32.lib
		advapi32.lib shell32.lib dbghelp.Lib gdi32.lib mysqlclient.lib
		libcurl.lib libeay32.lib ssleay32.lib)
else()
	if (ubuntu) #cmake -D ubuntu=1 ..
		set(EXTRA_LIBS_DIR "${LIB_PATH_NAME}/ubuntu/lib")
		set(EXTRA_DEFAULT_LIBS -lmysqlclient -lcurl -lm -ldl -lssl -lcrypto -lcrypt -lpthread -lstdc++)
	else() # suse
		set(EXTRA_LIBS_DIR "${LIB_PATH_NAME}/suse/lib")
		set(EXTRA_DEFAULT_LIBS -lmysqlclient -lcurl -ldl -lssl -lcrypto -lcrypt -lpthread)
	endif()
	set(COMPILE_DEFAULT_OPTION "-w -c -D_LINUX_ -lstdc++ -fPIC")
endif()

message("CMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}")
message("CMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE}")
message("CMAKE_BINARY_DIR=${CMAKE_BINARY_DIR}")
message("CMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR}")
message("PROJECT_BINARY_DIR=${PROJECT_BINARY_DIR}")

include_directories(
	"${CMAKE_CURRENT_SOURCE_DIR}/code/include/"
	"${CMAKE_CURRENT_SOURCE_DIR}/code/util/"
	"${CMAKE_CURRENT_SOURCE_DIR}/code/third_party/bounds_checking_function/include/"
	"${CMAKE_CURRENT_SOURCE_DIR}/code/third_party/"
	"${CMAKE_CURRENT_SOURCE_DIR}/code/"
	"${CMAKE_CURRENT_BINARY_DIR}/_deps/${LIB_CLI_NAME}-src/include"
)

add_definitions(${COMPILE_DEFAULT_OPTION})
link_directories(${CMAKE_INSTALL_LIBDIR} ${EXTRA_LIBS_DIR})
link_libraries(${EXTRA_DEFAULT_LIBS})
set(MODULE_LIB_TYPE STATIC)

if (GTEST_ENABLE)
include_directories(
	"${CMAKE_CURRENT_BINARY_DIR}/_deps/${LIB_GTEST_NAME}-src/googlemock/include"
	"${CMAKE_CURRENT_BINARY_DIR}/_deps/${LIB_GTEST_NAME}-src/googletest/include"
)
#set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/release)
set(LINK_STATIC_OPT ${LINK_STATIC_OPT} -fprofile-arcs -ftest-coverage -lgcov)
endif()

if (LIBYANG_ENABLE)
include_directories(
	"${CMAKE_CURRENT_BINARY_DIR}/_deps/${LIBYANG_NAME}-build"
)
endif()

add_subdirectory(code)

if (MSVC)
set_target_properties(judger PROPERTIES LINK_FLAGS /SUBSYSTEM:CONSOLE /NODEFAULTLIB:library /MACHINE:X86)
endif()

if (GTEST_ENABLE)
message("-- Gtest enable")
set(GTEST_EXTERN_LIBS gtest gmock)
add_definitions("-g -fsanitize=address -fsanitize-recover=address")
if (LIBYANG_ENABLE)
add_definitions("-DHAS_LIB_YANG")
set(GTEST_EXTERN_LIBS ${GTEST_EXTERN_LIBS} yang)
endif()
add_subdirectory(code/test)
add_executable(judger_tester code code/test/main/main.cpp)
target_link_libraries(judger_tester
	${LINK_STATIC_OPT}  -lstdc++ -lm -lasan
	testcase sysmng cmd cli ftp telnet util judge ic aaa event ndp cjson pcre crypt securec ${GTEST_EXTERN_LIBS} )
else()
add_executable(judger code code/main/main.cpp)
target_link_libraries(judger
	${LINK_STATIC_OPT} -lstdc++ -lm
	sysmng cmd cli ftp telnet util judge ic aaa event ndp cjson pcre crypt securec)
endif()